package com.other;

import java.io.*;

/**
 * Created by baiguantao on 2017/9/1.
 */
public class VideoUtils {

    public static void main(String[] args) throws IOException, InterruptedException {
        String filepath="d:\\xx.mp4";
        String thumbPath="d:\\thumb.png";
        VideoUtils.getThumb(filepath,thumbPath,800, 600, 0, 0, 9);
    }
    /****
     * 获取指定时间内的图片
     * @param videoFilename:视频路径
     * @param thumbFilename:图片保存路径
     * @param width:图片长
     * @param height:图片宽
     * @param hour:指定时
     * @param min:指定分
     * @param sec:指定秒
     * @throws IOException
     * @throws InterruptedException
     */
    public static void getThumb(String videoFilename, String thumbFilename, int width,
                         int height, int hour, int min, float sec) throws IOException,
            InterruptedException
    {
        ProcessBuilder processBuilder = new ProcessBuilder("D\\:\\\\zm\\\\ffmpeg\\\\bin\\\\ffmpeg.exe", "-y",
                "-i", videoFilename, "-vframes", "1", "-ss", hour + ":" + min
                + ":" + sec, "-f", "mjpeg", "-s", width + "*" + height,
                "-an", thumbFilename);
        String subpath=thumbFilename.substring(0,thumbFilename.lastIndexOf("/"));
        System.out.println(subpath);
        File tem=new File(subpath);
        if(!tem.exists()){
            tem.mkdirs();
        }
        Process process = processBuilder.start();

        InputStream stderr = process.getErrorStream();
        InputStreamReader isr = new InputStreamReader(stderr);
        BufferedReader br = new BufferedReader(isr);
        String line;
        while ((line = br.readLine()) != null)
            ;
        process.waitFor();

        if(br != null)
            br.close();
        if(isr != null)
            isr.close();
        if(stderr != null)
            stderr.close();
    }
}
